﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Web;
using System.Collections;
using System.IO;

namespace JTicket.Common
{
    public class CheckCode
    {
        /// <summary>
        /// 随机数数组
        /// </summary>
        /// <param name="Num">位数</param>
        /// <returns></returns>
        public static List<string> ListBarCode(int Num)
        {
            List<string> Returnlist = new List<string>();
            List<string> Pwdlist = new List<string>();
            byte _Result = 0;
            byte[] by;
            string getvalue, line;
            using (StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath("BarCode\\password.txt")))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.Length > 0)
                        Pwdlist.Add(line);
                    else
                        break;
                }
                sr.Close();
                sr.Dispose();
            }
            /****随机数定义****/
            Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
            string temp = string.Empty;
            int minValue = 100000000;
            int maxValue = 999999999;
            for (int i = 0; i < Num; i++)
            {
                //处理随机数
                temp = GetNum(Returnlist, ra.Next(minValue, maxValue).ToString(), minValue, maxValue, ra);
                by = Encoding.Default.GetBytes(temp);
                for (int j = 0; j <= by.Length - 1; j++)
                {
                    _Result ^= by[j];
                }
                getvalue = string.Format("{0}{1}{2}", temp.Substring(0, 3), Pwdlist[(int)_Result], temp.Substring(3));
                Returnlist.Add(getvalue); //取出值赋到数组中
            }
            return Returnlist;
        }
        /// <summary>
        /// 遍历随机数组
        /// </summary>
        /// <param name="arrNum"></param>
        /// <param name="tmp"></param>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <param name="ra"></param>
        /// <returns></returns>
        private static string GetNum(List<string> alist, string tmp, int minValue, int maxValue, Random ra)
        {
            int n = 0;
            while (n <= alist.Count - 1)
            {
                if (alist[n] == tmp) //利用循环判断是否有重复
                {
                    tmp = ra.Next(minValue, maxValue).ToString(); //重新随机获取。
                    GetNum(alist, tmp, minValue, maxValue, ra);//递归:如果取出来的数字和已取得的数字有重复就重新随机获取。
                }
                n++;
            }
            return tmp;
        }
        /// <summary>
        /// 生成随机数
        /// </summary>
        /// <returns></returns>
        public static string checkCode()
        {
            int number;
            char code;
            string checkCode = string.Empty;
            Random random = new Random();
            for (int i = 0; i < 4; i++)
            {
                number = random.Next();
                if (number % 2 == 0)
                {
                    code = (char)('0' + (char)(number % 10));
                }
                else
                {
                    code = (char)('A' + (char)(number % 26));

                }
                checkCode += code.ToString();
            }
            HttpCookie mycookie = new HttpCookie("CheckCode");
            mycookie["code"] = checkCode;
            mycookie.Expires = DateTime.Now.AddDays(1d);
            HttpContext.Current.Response.Cookies.Add(mycookie);
            return checkCode;
        }
        /// <summary>
        /// 生成数字验证码图片
        /// </summary>
        /// <param name="checkCode"></param>
        public static void CreateCheckCodeImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == string.Empty)
                return;
            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)System.Math.Ceiling(checkCode.Length * 12.5), 22);
            Graphics g = Graphics.FromImage(image);
            try
            {
                //生成随机生成器
                Random random = new Random();
                //清空图片背景
                g.Clear(Color.White);
                //画图片的背景噪音线
                for (int i = 0; i < 2; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Height);
                    int y1 = random.Next(image.Width);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
                }
                //字体
                Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(checkCode, font, brush, 2, 2);
                //画图片的前景噪音点
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));


                }
                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.ContentType = "image/Gif";
                HttpContext.Current.Response.BinaryWrite(ms.ToArray());


            }
            catch
            {
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }

        }

    }
}
